package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.uwm.universitydb.swing.DepartmentTableModel;

public class DepartmentMaint extends JPanel {
	
	public DepartmentMaint(DepartmentTableModel departmentTable) {
		_dataTable = departmentTable;
		this.setLayout(new BorderLayout());
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout());
		titlePanel.setBackground(Color.WHITE);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 16, 0, Color.WHITE));
		JLabel title = new JLabel("Department Maintenance");
		title.setFont(new Font(title.getFont().getName(), title.getFont().getStyle(), 18));
		titlePanel.add(title);

		this.add(titlePanel, BorderLayout.NORTH);
		this.add(this.createAppView(), BorderLayout.CENTER);
		
	}
	
	private JPanel createAppView() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		//Create Edit/Delete buttons panel
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		/*_editButton = new JButton(StudentMaint.EDITBUTTON);
		_editButton.setIcon(new ImageIcon("res/edit.png"));
		_editButton.setName(StudentMaint.EDITBUTTON);
		
		_deleteButton = new JButton(StudentMaint.DELBUTTON);
		_deleteButton.setIcon(new ImageIcon("res/remove.png"));
		_deleteButton.setName(StudentMaint.DELBUTTON);
		
		_addButton = new JButton(StudentMaint.ADDBUTTON);
		_addButton.setIcon(new ImageIcon("res/add.png"));
		_addButton.setName(StudentMaint.ADDBUTTON);
		
		btnPanel.setBackground(Color.WHITE);
		btnPanel.add(_addButton);
		//btnPanel.add(_editButton);
		btnPanel.add(_deleteButton);*/
		panel.add(btnPanel);

		//Create Table
		_jtable = new JTable(_dataTable);
		_jtable.setName("DataTbl");
		_jtable.setAlignmentX(Component.CENTER_ALIGNMENT);
		_jtable.getColumnModel().getColumn(0).setMaxWidth(200);
		_jtable.getColumnModel().getColumn(0).setMinWidth(200);
		_jtable.getTableHeader().setReorderingAllowed(false);
		
		JScrollPane tblPane = new JScrollPane(_jtable);
		tblPane.getViewport().setBackground(Color.WHITE);
		panel.add(tblPane);
			
		panel.add(Box.createRigidArea(new Dimension(0,16)));
		return panel;
	}
	
	public JPanel showMaintView(String department) {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		//Name
		panel.add(new JLabel("Department"));
		JTextField nameField = new JTextField(department);
		nameField.setName(DEPARTMENT);
		nameField.setEditable(false);
		panel.add(nameField);
		
		return panel;
	}
	
	public JPanel showNewView() {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		//Name
		panel.add(new JLabel("Department"));
		JTextField name = new JTextField();
		name.setName(DEPARTMENT);
		panel.add(name);
		
		return panel;
	}
	
	public void refreshTable() {
	    _jtable.revalidate();
	    _jtable.repaint();		
	}
	
	public JButton getAddButton() {
		return _addButton;
	}
	
	public JButton getEditButton() {
		return _editButton;
	}
	
	public JButton getDelButton() {
		return _deleteButton;
	}
	
	public JTable getDataTable() {
		return _jtable;
	}
	
	
	private JButton _editButton;
	private JButton _deleteButton;
	private JButton _addButton;
	private JTable _jtable;
	
	private DepartmentTableModel _dataTable;
	
	public static final String EDITBUTTON = "Edit";
	public static final String DELBUTTON = "Delete";
	public static final String ADDBUTTON = "New";
	
	public static final String DEPARTMENT = "DEPARTMENT";
	public static final String PASSWORDFIELD = "PASSWORD";
	public static final String ROLEFIELD = "ROLE";
	
	private static final long serialVersionUID = -8837812633257474913L;
}
